package fun.werfamily.wf.payment.core.controller;

import fun.werfamily.wf.base.vo.Result;
import fun.werfamily.wf.payment.core.domain.dto.req.PayCancelReq;
import fun.werfamily.wf.payment.core.domain.dto.req.PayCreateReq;
import fun.werfamily.wf.payment.core.domain.dto.resp.PayCancelSuccessRespDTO;
import fun.werfamily.wf.payment.core.domain.dto.resp.PaySuccessRespDTO;
import fun.werfamily.wf.payment.core.domain.service.impl.AliPayService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Mr.WenMing Zhang
 * @Date: 2023-05-02 20:31
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/pay")
public class AlipayController {

    @Resource
    private AliPayService aliPayService;

    @ApiOperation("创建支付单")
    @PostMapping("/create")
    public Result<PaySuccessRespDTO> createPay(@RequestBody PayCreateReq req) {
        log.info("创建支付单req={}", req);
        PaySuccessRespDTO pay = aliPayService.createPay(req);
        return Result.success(pay);
    }

    @ApiOperation("取消支付支付")
    @PostMapping("/camcel")
    public Result<PayCancelSuccessRespDTO> cancelPay(@RequestBody PayCancelReq req) {
        log.info("创建支付单req={}", req);
        PayCancelSuccessRespDTO cancelPay = aliPayService.cancelPay(req);
        return Result.success(cancelPay);
    }

    /**
     * 支付异步通知
     * 接收到异步通知并验签通过后，一定要检查通知内容，包括通知中的app_id、out_trade_no、total_amount是否与请求中的一致，并根据trade_status进行后续业务处理。
     * https://docs.open.alipay.com/194/103296
     *
     * @param request
     * @return
     */
    @PostMapping("/open/alipay/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        aliPayService.notify(request, response);
    }

    /**
     * 交易查询
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/alipay/query")
    public Result<String> query(@RequestParam("orderNo") String orderNo) {
        return Result.success(aliPayService.getPayStatus(orderNo));
    }
}
